<template>
  <div id="app" class="skBmiInfo">
    <a-card :bordered="true">
      <sk-life-table-header @dataFrom="getDataFrom" v-bind="$attrs" ref="skTableHeader" :classId="classId"></sk-life-table-header>
      <div class="ant-card-head-wrapper">
        <div class="ant-card-head-title">
          <span class="subtitle" style="margin-bottom: 14px;">数据时间：{{dataDate}}</span>
        </div>
        <div class="ant-card-extra">
          <div class="left-circle-icon">
            <a-icon type="left-circle"  @click="closeTable" style="font-size: 22px;"/>
          </div>
        </div>
      </div>
      <bar
        :barName="barName"
        :barData="barData"
        :color="['#A9F387', '#48D8BF']"
        :barWidth="10"
        :symbolSize="5"
        :width="'100%'"
        :seriesName="'人数（人）'"
        :unit="'人'"
        :height="'380px'"
        :dataZoomShow="false"
      ></bar>
      <a-row :gutter="[12, 28]">
        <a-col>
          <a-table :loading="loading" :columns="columns" :data-source="dataSource" bordered :scroll="{x:2000,y:1200}">
          </a-table>
        </a-col>
      </a-row>
      <sk-life-student-table v-show="classId !=''" v-bind="$attrs" :detailName="detailName" ref="skLifeStudentTable" :dataForm="dataForm"></sk-life-student-table>
    </a-card>
  </div>
</template>
<script>
import { getAction } from '@/api/manage'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil';
import skLifeTableHeader from "./skTableHeader.vue";
import skLifeStudentTable from "./skStudentTable.vue";
import Bar from '@/components/Echarts/Bar.vue'
export default {
  name: 'skBmiInfo',
  data() {
    return {
      url: {
        list: 'health/skBmiRecord/queryBmi'
      },
      loading:false,
      dataDate: '',
      dataForm:{},
      months: [],
      columns:[],
      dataSource:[],
      classId:"",
      rowKey:"classId",
      barName:[],
      barData:[]
    }
  },
  props:["detailName"],
  components: {
    skLifeTableHeader,
    skLifeStudentTable,
    Bar
  },

  filters: {},

  computed: {},
  activated() {
  },
  
  created() {
    this.dataDate = this.$skCommon.getDate(1);
  },

  mounted() {
  },
  
  methods: {
    // 关闭表格页面
    closeTable(){
      let data = {
        show:true
      }
      this.$emit("cardData",data);
    },
    // 获取参数
    getDataFrom(dataForm){
      console.log(dataForm);
      this.classId = dataForm.classId;
      this.setColumns(dataForm.semesterId ? dataForm.semesterId : dataForm.semesterHistoryId);
      this.getDetail(dataForm);
      this.$nextTick(()=>{
        if (dataForm.classId != "") {
          this.$refs.skLifeStudentTable.getStudentData(dataForm, this.months);
        }
      })
      this.queryBmiHeight(dataForm)
    },
    //查询身高体重 
    queryBmiHeight(dataForm){
      this.barName = []
      this.barName.push("体重偏瘦","体重正常","体重偏胖","体重肥胖","身高偏矮","身高正常","身高偏高")
      const url = "health/skBmiRecord/queryBmiHeightDistribution"
      const params = {
        classId:dataForm.classId,
        semesterId:dataForm.semesterId
      }
      getAction(url,params).then((res)=>{
      if(res.success){
        this.barData = [];
        if (res.result && res.result.length > 0) {
          if (dataForm.classId) {
            let data =  res.result[0]
            this.barData.push(data.bmiThinNum,data.bmiNormalNum,data.bmiOverweightNum,data.bmiObesityNum,data.heightShortNum,data.heightNormalNum,data.heightTallNum)
          }else{
            let bmiThinNum = 0,       // 体重偏瘦人数
              bmiNormalNum = 0,        // 体重正常人数
              bmiOverweightNum = 0,    // 体重偏胖人数
              bmiObesityNum = 0,      // 体重肥胖人数
              heightShortNum = 0,      // 身高偏矮人数
              heightNormalNum = 0,     // 身高正常人数
              heightTallNum = 0;      // 身高偏高人数
            res.result.forEach(item => {
              bmiThinNum += item.bmiThinNum;
              bmiNormalNum += item.bmiNormalNum;
              bmiOverweightNum += item.bmiOverweightNum;
              bmiObesityNum += item.bmiObesityNum;
              heightShortNum += item.heightShortNum;
              heightNormalNum += item.heightNormalNum;
              heightTallNum += item.heightTallNum;
            });
            this.barData.push(bmiThinNum,bmiNormalNum,bmiOverweightNum,bmiObesityNum,heightShortNum,heightNormalNum,heightTallNum)
          }
        }
      } else {
        this.$message.warning(res.message);
      }
      }).catch((error)=>{
        console.error(error);
      })
    },
    // 详情
    getDetail(dataForm) {
      this.dataForm = dataForm;
      const params = {
        statisticalScope: 'class',
        semesterId: dataForm.semesterId ? dataForm.semesterId : dataForm.semesterHistoryId,
        classId: dataForm.classId ? dataForm.classId : undefined
      }

      this.loading = true;
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            this.dataSource = [];
            let classList = this.getClassList(dataForm.classId);
            console.log(classList);
            classList.forEach(el => {
              let classItem = {
                classId: el.id,
                className: el.className,
                classType: el.classType
              }
              const classData = res.result.filter(item => {
                return item.classId == el.id
              })

              for(let rowIndex = 0; rowIndex < this.months.length; rowIndex++){
                const rowString = this.months[rowIndex].toString()
                const monthData = classData.find(item => (item.year + '-' + item.month) == rowString);
                if(monthData){
                  classItem['avgHeight' + rowString] = monthData.avgHeight;
                  classItem['avgWeight' + rowString] = monthData.avgWeight;
                }else{
                  classItem['avgHeight' + rowString] = '-';
                  classItem['avgWeight' + rowString] = '-';
                }
              }
              
              this.dataSource.push(classItem);
            })
            this.loading = false;
          } else {
            this.$message.warning(res.message);
            this.loading = false;
          }
        })
        .catch(error => {
          console.error(error)
          this.loading = false;
        })
    },
    getClassList(classId) {
      let classList = this.$refs.skTableHeader.classListData()
      if(classId){
        classList = classList.filter(item => {
          return item.id == classId
        })
      }
      return classList;
    },
    // 表头
    setColumns(semesterId){
      this.columns = [
        {
          title: '年级',
          align: 'center',
          dataIndex: 'classType',
          fixed: 'left',
          key: 'classType',
          width: 80,
          customRender: function(text) {
            return filterDictTextByCache('class_type', text)
          }
        },
        {
          title: '班级',
          align: 'center',
          fixed: 'left',
          dataIndex: 'className',
          key: 'className',
          width: 100,
        },
      ];
      
      let semester = this.$refs.skTableHeader.getSemesterDateSE(semesterId);
      this.months = this.$skCommon.getMonths(semester.startDate, semester.endDate, 'yyyy-MM');

      for(let rowIndex = 0; rowIndex < this.months.length; rowIndex++){
        const rowString = this.months[rowIndex].toString()
        this.columns.push(
          {
            title: rowString,
            children: [
              {
                title: '平均身高(cm)',
                dataIndex: 'avgHeight'+rowString,
                key: 'avgHeight'+rowString,
                width: this.months.length > 6 ? 120 : 'unset',
              },
              {
                title: '平均体重(kg)',
                dataIndex: 'avgWeight'+rowString,
                key: 'avgWeight'+rowString,
                width: this.months.length > 6 ? 120 : 'unset',
              },
            ],
          },
        )
      }
      console.log('columns', this.columns);
    },
  }
}
</script>
<style scoped lang="less">
/* @import url(); 引入css类 */
  @import '~@assets/less/skReport.less';
</style>
